package com.ruoyi.web.controller.purchase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.Brand;
import com.ruoyi.purchase.domain.Firm;
import com.ruoyi.purchase.domain.Product;
import com.ruoyi.purchase.domain.Type;
import com.ruoyi.purchase.service.BrandService;
import com.ruoyi.purchase.service.TypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("purchase/type")
public class TypeController extends BaseController {
    private String prefix = "purchase/type";

    @Autowired
    private TypeService typeService;

    @Autowired
    private BrandService brandService;


    /**
     * 根据品牌id查询类型
     * @return
     */
    @GetMapping("/typeList")
    @ResponseBody
    public List<Type> typeList(Integer brandId){
        System.out.println("品牌Id："+brandId);
        Type type=new Type();
        type.setPoBrandId(brandId);
        return typeService.findAllType(type);
    }

    @RequiresPermissions("purchase:type:view")
    @GetMapping()
    public String Type(Model model)
    {

        List<Type> typeList = typeService.list();
        List<Brand> brandList=brandService.list();
        model.addAttribute("brandList",brandList);
        model.addAttribute("typeList",typeList);
        return prefix + "/type";
    }
    /**
     * 查询商品类型信息列表
     */
    @RequiresPermissions("purchase:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Type type)
    {
        startPage();
        List<Type> list = typeService.queryList(type);
        return getDataTable(list);
    }

    /**
     * 导出商品类型信息列表
     */
    @RequiresPermissions("purchase:type:export")
    @Log(title = "商品类型信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Type type)
    {
        List<Type> list = typeService.queryList(type);
        ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 增加商品类型信息
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<Type> typeList = typeService.list();
        List<Brand> brandList = brandService.list();
        model.addAttribute("brandList",brandList);
        model.addAttribute("typeList",typeList);
        return prefix + "/add";
    }

    /**
     * 新增保存商品类型信息
     */
    @RequiresPermissions("purchase:type:add")
    @Log(title = "商品类型信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Type type) { return toAjax(typeService.save(type)); }

    /**
     * 删除厂商信息
     */
    @RequiresPermissions("purchase:type:remove")
    @Log(title = "品牌信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(typeService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 修改商品类型信息
     */
    @GetMapping("/edit/{tid}")
    public String edit(@PathVariable("tid") Integer typeId, ModelMap mmap)
    {
        Type type = typeService.getById(typeId);
        SysUser user = getSysUser();//当前登录用户信息
        List<Type> typeList = typeService.list();
        List<Brand> brandList = brandService.list();
        mmap.addAttribute("brandList",brandList);
        mmap.put("user",user);
        mmap.addAttribute("typeList",typeList);
        mmap.put("type", type);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品类型信息
     */
    @RequiresPermissions("purchase:type:edit")
    @Log(title = "品牌信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Type type)
    {
        return toAjax(typeService.updateById(type));
    }

    /**
     * 用户状态修改
     */
    @Log(title = "厂商管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("purchase:type:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Type type)
    {
        return toAjax(typeService.changeStatus(type));
    }


    @RequiresPermissions("purchase:type:detail")
    @GetMapping("/detail/{typeId}")
    public String detail(@PathVariable("typeId") Integer typeId, ModelMap mmap)
    {
        List<Type> typeList = typeService.list();
        List<Brand> brandList = brandService.list();
        SysUser user = getSysUser();//当前登录用户信息
        mmap.put("user",user);
        mmap.addAttribute("brandList",brandList);
        mmap.addAttribute("typeList",typeList);
        mmap.put("type", typeService.selectTypeById(typeId));
        return prefix + "/detail";
    }



}
